package org.dinguo.orm;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;

public class TableConveter {

	private Connection conn = null;

	private final String URL = "jdbc:mysql://localhost:3306/xncdb?useUnicode=true&characterEncoding=GBK";

	private final String USER_NAME = "root";

	private final String PASSWORD = "11111111";

	public static void main(String[] args) throws Exception {
		TableConveter tc = new TableConveter();
		tc.mapperTable("xnc_forum_forum");
//		tc.getTableProperty("ZFZVF", "JG_NJQK","select * from xnc_forum_forum"); 
		tc.close();
		
	}

	public TableConveter() throws Exception {
		Class.forName("com.mysql.jdbc.Driver");
		if (conn == null || conn.isClosed()) {
			conn = DriverManager.getConnection(URL, USER_NAME, PASSWORD);
		}
	}

	public void close() throws Exception {
		if (conn != null && !conn.isClosed()) {
			conn.close();
		}
	}

	public void mapperTable(String tableName) throws Exception {

		String sql = "select * from " + tableName;
		PreparedStatement ps = conn.prepareStatement(sql);
		ResultSet result = ps.executeQuery();
		ResultSetMetaData metaData = result.getMetaData();
		int count = metaData.getColumnCount();
		for (int i = 1; i < count; i++) {
			String label = metaData.getColumnLabel(i);
			String name = metaData.getColumnName(i);
			int type = metaData.getColumnType(i);
			String tn = metaData.getColumnTypeName(i);
			System.out.println(label + "\t" + name + "\t" + tn + "\t" + type);
		}
	}
	
	/** 
     * 获取表到Java Bean的成员列表的Java代码 
     * @param schema 模式 
     * @param tbName 表名 
     * @return Java Bean的成员列表的Java代码 
     */ 
    public String getTableProperty(String schema, String tbName,String tb_sql) { 
        StringBuffer sb = new StringBuffer(); 
        sb.append(schema).append(".").append(tbName).append("\n"); 
        try { 
        	System.out.println(tb_sql);
            PreparedStatement pstmt = conn.prepareStatement(tb_sql); 
            pstmt.setString(1, schema); 
            pstmt.setString(2, tbName); 
            ResultSet rs = pstmt.executeQuery(); 
            while (rs.next()) { 
                String type = typeProcessor(rs.getString(1).trim()); 
                String name = rs.getString(2).toLowerCase(); 
                String remark; 
                if (rs.getString(3) == null) { 
                    remark = ""; 
                } else { 
                    remark = rs.getString(3); 
                } 
                sb.append("\tprivate\t" + type + "\t" + name + ";\t\t\t//" + remark + "\n"); 
            } 
            rs.close(); 
            pstmt.close(); 
        } catch (SQLException e) { 
            e.printStackTrace(); 
        } finally { 
            try { 
                conn.close(); 
            } catch (SQLException e) { 
                e.printStackTrace(); 
            } 
        } 
        System.out.println(sb.toString()); 
        return sb.toString(); 
    } 

    /** 
     * SQL数据类型到Java数据类型的转换 
     * @param sqlType SQL数据类型 
     * @return Java数据类型 
     */ 
    public String typeProcessor(String sqlType) { 
        String newType; 
        if (sqlType.equalsIgnoreCase("varchar")) newType = "String"; 
        else if (sqlType.equalsIgnoreCase("char")) newType = "String"; 
        else if (sqlType.equalsIgnoreCase("bigint")) newType = "Long"; 
        else if (sqlType.equalsIgnoreCase("smallint")) newType = "int"; 
        else if (sqlType.equalsIgnoreCase("integer")) newType = "int"; 
        else if (sqlType.equalsIgnoreCase("decimal")) newType = "double"; 
        else if (sqlType.equalsIgnoreCase("timestmp")) newType = "Date"; 
        else { 
            newType = sqlType; 
        } 
        return newType; 
    } 
}

